package com.twotwoshop.service.search.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.twotwoshop.commom.es.EsConstants;
import com.twotwoshop.commom.rabbitmq.RabbitConstants;
import com.twotwoshop.commom.redis.RedisConstants;
import com.twotwoshop.domin.goods.vos.ProductDetailsVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Component
@Slf4j
@RequiredArgsConstructor
public class SearchGoodsListener {


    private final StringRedisTemplate stringRedisTemplate;

    private final RestHighLevelClient client;


    /**
     * 监听es索引创建消息
     */
    @RabbitListener( bindings = @QueueBinding(
            value = @Queue(name = RabbitConstants.TWOTWO_SEARCH_TOPIC),
            exchange = @Exchange(name = RabbitConstants.TWOTWO_SEARCH_EXCHANGE,type = ExchangeTypes.DIRECT)
    ))
    @Transactional
    public void onMessage(Map<String, Object> esDocument){

        //如果获取成功
        if (esDocument!=null){

            //如果为添加热点商品
            if (esDocument.get(RabbitConstants.ACTION).equals(RabbitConstants.ACTION_HOT)){

                //保存热点数据进Redis
                saveGoodsRedis(esDocument);

            } //如果添加新发布商品
            else if (esDocument.get(RabbitConstants.ACTION).equals(RabbitConstants.ACTION_NEW)){





            }  //如果为添加推荐商品
            else if (esDocument.get(RabbitConstants.ACTION).equals(RabbitConstants.ACTION_COMMENT)){



            }




        }else{
            log.error("获取用户凭证失败");
        }

    }



    //1.写入商品浏览数据到Redis
    public void saveGoodsRedis(Map<String, Object> esDocument) {

        //删除rabbti识别的key 防止存入报错
        esDocument.remove(RabbitConstants.ACTION);

        //获取获取货物的id
        String id = (String)esDocument.get("id");
        String views = (String)esDocument.get("views");


        // 获取ZSetOperations
        ZSetOperations<String, String> zSetOperations = stringRedisTemplate.opsForZSet();


        // 获取一个合适的分数，
        long score = Long.parseLong(views)+1L;


        // 将商品浏览信息存入Redis的有序集合
        zSetOperations.add(RedisConstants.GOODS_HOT_KEY, id, score);


        // 为商品数据设置TTL
        stringRedisTemplate.expire(RedisConstants.GOODS_HOT_KEY, RedisConstants.GOODS_HOT_TTL, TimeUnit.MINUTES);
    }





}












